package com.huixin.zhang.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.huixin.zhang.persistence.dto.ExcelHeaderDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class ReadExcelDataUtils {
    /**
     * 表头配置
     * @param header
     * @param headers
     */
    public static void configExcelHeader(HSSFRow header, String[] headers){
        for (int i = 0; i < headers.length; i++) {
            header.createCell(i).setCellValue(headers[i]);
        }
    }

    /**
     * 表头配置
     * @param header
     * @param excelHeaderDtos
     */
    public static void configExcelHeader(HSSFRow header, ExcelHeaderDto[] excelHeaderDtos){
        for (int i = 0; i < excelHeaderDtos.length; i++) {
            header.createCell(i).setCellValue(excelHeaderDtos[i].getHeadName());
        }
    }

    /**
     * 获取属性对应的值
     * @param propertyName
     * @param entity
     * @return
     */
    public static Object getValueByPropertyName(String propertyName,Object entity){
        String startStr = propertyName.substring(0, 1).toUpperCase();
        String getter = "get" + startStr + propertyName.substring(1);
        Method method = null;
        try {
            method = entity.getClass().getMethod(getter);
            Object val = method.invoke(entity);
            if(propertyName.contains("Time")) {
                return val == null?"":new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)val);
            }
            return val == null?"":val;
        } catch (Exception e) {
            log.info("获取属性值失败");
        }
        return null;
    }

    /**
     * 获取属性名
     * @param entity
     * @return
     */
    public static String[] getFiledName(Object entity) {
        Field[] fields = entity.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    public static List<Map<String, Object>> getFiledNameValue(List<?> entitys) throws IllegalAccessException {
        List<Map<String, Object>> list = new ArrayList<>();
        for(int i = 0; i < entitys.size(); i ++){
            Object entity = entitys.get(i);
            JSONObject json = JsonUtils.toJsonObject(entity);
            Field[] fields = entity.getClass().getDeclaredFields();
            Map<String, Object> map = new HashMap<String, Object>();
            for (int j = 0; j < fields.length; j++) {
                fields[j].setAccessible(true);
                String field = fields[j].getName();
                String val = json.getString(field);
                map.put(fields[j].getName(), StringUtils.isBlank(val) ? "" : val);
            }
            list.add(map);
        }
        return list;
    }
}
